/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hiruss.efair.servlets;

import com.hiruss.efair.entities.Advertisement;

import com.hiruss.efair.persistence.AdvertisementManager;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hiru
 */
public class SearchAdvertisementServlet extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				AdvertisementManager manager = AdvertisementManager.getInstance();
				List<Advertisement> ads;
				if (req.getParameter("cat") != null) {
						ads = manager.getAdvertisementsByCategory(req.getParameter("cat").replace('_', ' '));
						req.setAttribute("title", "Products related to '" + req.getParameter("cat").replace('_', ' ') + "'");
				} else if (req.getParameter("item") != null) {
						ads = manager.getAdvertisementsByItem(req.getParameter("item"));
						req.setAttribute("title", "Products related to '" + req.getParameter("item") + "'");
				} else {
						ads = manager.getAllAdvertisements();
				}
				req.setAttribute("results", ads);
				req.getRequestDispatcher("searchResults.jsp").forward(req, resp);
		}
}
